import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:ty_smart_watch/ble/ble_util.dart';
import 'package:ty_smart_watch/ble/connected_device_controller.dart';
import 'package:ty_smart_watch/common/function_id.dart';
import 'package:ty_smart_watch/common/images_url.dart';
import 'package:ty_smart_watch/page/device/device_info_controller.dart';
import 'package:ty_smart_watch/page/me/controller/user_detail_controller.dart';

// import 'package:ty_smart_watch/ble/connected_device_controller.dart';
import 'package:ty_smart_watch/page/smartlife/clock.dart';
import 'package:ty_smart_watch/page/smartlife/controller/body_grid_controller.dart';
import 'package:ty_smart_watch/page/smartlife/controller/connected_device_controller.dart';
import 'package:ty_smart_watch/page/smartlife/controller/weather_controller.dart';
import 'package:ty_smart_watch/page/smartlife/get_geo_locator.dart';
import 'package:ty_smart_watch/page/smartlife/today_overview.dart';
import 'package:ty_smart_watch/page/sport/controller/sport_controller.dart';
import 'package:ty_smart_watch/utils/Log.dart';
import 'package:ty_smart_watch/utils/weather_util.dart';

import '../scandevices/controller/scan_device_data.dart';
import 'body_grid.dart';
import 'device_list.dart';

/// 智慧生活页面

class SmartLifePage extends StatefulWidget {
  final Function(int)? onPageChanged;

  const SmartLifePage({Key? key, this.onPageChanged}) : super(key: key);

  @override
  State<SmartLifePage> createState() => _SmartLifePageState();
}

class _SmartLifePageState extends State<SmartLifePage> {
  Function(int)? onPageChanged;

  @override
  Widget build(BuildContext context) {
    initController();
    Log.d("SmartLifePage build");
    onPageChanged = widget.onPageChanged;
    return Scaffold(
      body: ListView(
        children: [
          Title(),
          const DeviceList(),
          const TodayOverview(),
          const Clock(),
          BodyGrid(onPageChanged: onPageChanged)
        ],
      ),
    );

    // return PopScope(
    //   canPop: false,
    //   onPopInvoked: (bool didPop) {
    //     Log.d("did pop : $didPop");
    //     if (didPop) {
    //       return; // really exit
    //     } else {
    //       // Navigator.of(context).pop();
    //     }
    //   },
    //   child: Scaffold(
    //     body: ListView(
    //       children: [Title(), DeviceList(), TodayOverview(), Clock(), BodyGrid()],
    //     ),
    //   ),
    // );
  }

  void initController() {
    Get.put(BodyGridController());
    Get.put(DeviceInfoController());
    Get.put(ScanDeviceController());
    Get.put(DeviceController());
    Get.put(SportController());
  }
}

//顶部个人信息
class Title extends StatefulWidget {
  Title({super.key});

  @override
  State<Title> createState() => _TitleState();
}

class _TitleState extends State<Title> {
  int i = 0;

  @override
  Widget build(BuildContext context) {
    Get.put(WeatherController());
    UserDetailController _controller = Get.put(UserDetailController());
    return Container(
      margin: const EdgeInsets.fromLTRB(20, 20, 20, 20),
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          GestureDetector(
            child: Image.asset(
              SETTING_AVATAR,
              width: 36,
              height: 36,
            ),
            onTap: () {
              Log.d("i: $i");
            },
          ),
          //头像和名称的间隔
          Container(width: 10),
          GestureDetector(
            child: Obx(
              () => Text(
                _controller.userName.value,
                style: const TextStyle(fontWeight: FontWeight.w600),
              ),
            ),
            onTap: () {
              // i = i + 2;
              BleUtil.disconnectAllDevice();
            },
          ),
          Expanded(child: Container()),
          Temperature()
        ],
      ),
    );
  }
}

//天气组件
class Temperature extends StatelessWidget {
  Temperature({super.key});

  final WeatherController weatherController = Get.find();

  @override
  Widget build(BuildContext context) {
    return FutureBuilder(
        future: getLocationPermission(),
        builder: (ctx, snap) {
          return Column(
            crossAxisAlignment: CrossAxisAlignment.end,
            children: [
              Obx(() => Text(
                    weatherController.zone.value,
                    style: const TextStyle(
                        color: Colors.black, fontSize: 10, fontWeight: FontWeight.w700),
                  )),
              Row(
                children: [
                  Obx(() => Text(weatherController.mode.value,
                      style: const TextStyle(color: Colors.black, fontSize: 10))),
                  Container(width: 4),
                  Obx(() => Text("${weatherController.temperature.value}℃",
                      style: const TextStyle(color: Colors.black, fontSize: 10))),
                ],
              )
            ],
          );
        });
  }

  getLocationPermission() async {
    var getGeoLocator = GetGeoLocator();
    await Permission.location.onDeniedCallback(() {
      //拒绝
      print("onDeniedCallback");
    }).onGrantedCallback(() {
      //允许
      print("onGrantedCallback");
      getGeoLocator.getLocation();
    }).onPermanentlyDeniedCallback(() {
      //永久拒绝
      print("onPermanentlyDeniedCallback");
    }).onRestrictedCallback(() {
      //限制 ???
      print("onRestrictedCallback");
    }).onLimitedCallback(() {
      //有限 ???
      print("onLimitedCallback");
    }).onProvisionalCallback(() {
      //临时 ???
      print("onProvisionalCallback");
    }).request();
  }
}
